package org.dizena.blog.starter.web;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.ttl.TtlRunnable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dizena.blog.starter.entity.User;
import org.dizena.blog.starter.feign.AuthFeign;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequestMapping("/blog")
@Tag(name = "Blog接口")
public class BlogController {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private AuthFeign authFeign;


    @Operation(summary = "异步请求")
    @GetMapping({"/test/{cmd}"})
    public String test(@PathVariable("cmd") String cmd) {
        User user = new User();
        user.setAge(18);
        user.setName("POST");
        user.setId(1001L);
        HttpHeaders headers = new HttpHeaders();
        headers.set("sid", "100010001");
        HttpEntity<User> body = new HttpEntity<>(user, headers);

        if ("lbget".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {
                User res = restTemplate.getForObject("http://mod-auth/auth/get", User.class);
                log.info("lb  get user {}", res);
            }));
        }
        if ("lbpost".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {

                User res3 = restTemplate.exchange("http://mod-auth/auth/post", HttpMethod.POST, body, User.class).getBody();
                log.info("lb  post user {}", res3);
            }));
        }


        if ("ipget".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {
                User res2 = restTemplate.getForObject("http://127.0.0.1:9000/auth/get", User.class);
                log.info("ip  get user {}", res2);
            }));
        }

        if ("ippost".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {
                User res4 = restTemplate.exchange("http://127.0.0.1:9000/auth/post", HttpMethod.POST, body, User.class).getBody();
                log.info("ip  post user {}", res4);
            }));
        }

        if ("feignget".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {
                HttpEntity<User> resp = authFeign.get();
                log.info("feign get user {}", resp.getBody());
            }));
        }
        if ("feignpost".equals(cmd) || "all".equals(cmd)) {
            ThreadUtil.execute(TtlRunnable.get(() -> {
                HttpEntity<User> resp2 = authFeign.post(user);
                log.info("feign post user {}", resp2.getBody());
            }));
        }

        return "子线程传数据测试";
    }

    @Operation(summary = "异常请求")
    @GetMapping({"/demo"})
    public String demo() {
        log.info("demo");
        String s = null;
        if (s.length() > 0) {

        }
        return s;
    }


}
